#include "../main.h"

#define PATH_FIFO "/home/yujiu/fifo"

int main()
{
    int res=0;
    res=access(PATH_FIFO,F_OK);
    if(res==-1){
        perror("access");
        exit(EXIT_FAILURE);
    }
    int fd=open(PATH_FIFO,O_RDONLY);
    if(-1==fd){
        perror("open");
        exit(EXIT_FAILURE);
    }
    char buf[128];
    ssize_t byte =0;
    struct pollfd fds[2];
    (*fds).fd=fd;
    (*fds).events=POLLIN;
    (*(fds+1)).fd=0;
    (*(fds+1)).events=POLLIN;
    while(1){
        res=poll(fds,2,3000);
        if(res==-1){
            perror("pool");
            close(fd);
            exit(EXIT_FAILURE);
        }else if(0==res){
            printf("timeout\n");
        }else{
            if(fds[0].revents==POLLIN){
                memset(buf,0,sizeof(buf));
                byte=read(fd,buf,sizeof(buf));
                printf("read:%s\n",buf);
                if(strncmp(buf,"exit",4)==0){
                    close(fd);
                    exit(EXIT_SUCCESS);
                }
            }
            if(fds[1].revents==POLLIN){
                memset(buf,0,sizeof(buf));
                fgets(buf,sizeof(buf),stdin);
                buf[strlen(buf)-1]='\0';
                printf("read:%s\n",buf);
                if(strncmp(buf,"exit",4)==0){
                    close(fd);
                    exit(EXIT_SUCCESS);
                }
            }
        }
    }
    close(fd);
    return 0;
}

